<?php

namespace backend\modules\mall\controllers;

use Yii;
use backend\extensions\ApiBaseController;
use backend\services\mall\MallPageService;

/**
 * 专题
 */
class PageController extends ApiBaseController
{
    /**
     * 列表
     * * */
    public function actionList()
    {
        $params = Yii::$app->request->isPost ? Yii::$app->request->post() : Yii::$app->request->get();
        $rules = [
            [['name', 'state', 'is_home'], 'default', 'value' => 0, 'message' => '名称检索'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 10000, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        
        $data = MallPageService::getList($this->params);
        return $this->jsonSuccess($data);
    }

   /**
     * 保存
     * * */
    public function actionSave()
    {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name', 'key'], 'required', 'message' => '{attribute}属必填项'],
            [['id', 'info'], 'default', 'value' => '', 'message' => '非必填参数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if ($this->params['id']) {
            return MallPageService::update($this->params);
        }
        unset($this->params['id']);
        return MallPageService::create($this->params);
    }


    /**
     * 详情
     * * */
    public function actionDetail()
    {
        $params = Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $info = MallPageService::detail($this->params);
        return $this->jsonSuccess($info);
    }

    /**
     * 删除
     * * */
    public function actionDel()
    {
        $params = Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return MallPageService::delete($this->params);
    }

    /**
     * 删除
     * * */
    public function actionPageGoods() {
        $params = Yii::$app->request->post();
        $rules = [
            [['ids'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return MallPageService::pageGoodsList($this->params);
    }

}
